/*************************************************************************
	> File Name: oledHex.c
	> Author: 
	> Mail: 
	> Created Time: 2016年12月22日 星期四 20时23分12秒
 ************************************************************************/

#include<stdio.h>
#include <stdlib.h>
#include <string.h>

unsigned char getBit(unsigned char value)
{
#if 0
    unsigned char res = (value < 128) ? 1 : 0;
#else
     unsigned char res = (value < 128) ? 0 : 1;
#endif
    /*printf("%d", res);*/
    return res;
}

int main(int argc, char **argv)
{
    if (argc != 2) {
        printf("usage: %s bmpfile\n", argv[0]);
        return -1;
    }

    FILE *bmp = fopen(argv[1], "rb");
    short bitDepth = 0;
    short bmpHead = 0;

    fread(&bmpHead, 2, 1, bmp);
    if (bmpHead != 0x4d42) {
        printf("not bmp format\n");
        return -1;
    }

    fseek(bmp, 0x1c, SEEK_SET);
    fread(&bitDepth, 2, 1, bmp);
    if (bitDepth != 0x18) {
        printf("%d\n", bitDepth);
        printf("support 24bits bmp only\n");
        return -1;
    }

    int width = 0;
    int height = 0;
    fseek(bmp, 0x12, SEEK_SET);
    fread(&width, sizeof(int), 1, bmp);
    fread(&height, sizeof(int), 1, bmp);
    //printf("input file info: \n");
   // printf("filename %s\n",argv[1]);
//    printf("size = %dx%d\n", width, height);
    int w = width;
    int h = height / 8 + 1;
    int fill = 8 - (height % 8);
   // printf("filling %d row\n", fill/2);

//new add
#if 1
    h = height / 8 ;
    fill = 0;
    char *pcStr =0;
    char file_name[256];
    char tmp[16];

    snprintf(tmp,sizeof(tmp),"%d_%d",width,height);
    memset(file_name,0,sizeof(file_name));
    snprintf(file_name,sizeof(file_name),"%s",argv[1]);
    //printf("file_name %s\n",file_name);

    while((pcStr = strstr(file_name,",")) != 0)
    {
        pcStr[0] = '_';
    }
    while((pcStr = strstr(file_name,"-")) != 0)
    {
        pcStr[0] = '_';
    }
    while((pcStr = strstr(file_name,"(")) != 0)
    {
        pcStr[0] = '_';
    }
    while((pcStr = strstr(file_name,")")) != 0)
    {
        pcStr[0] = '_';
    }
    if((pcStr = strstr(file_name,".bmp")) != 0)
    {
        //strcat(pcStr,tmp);
        sprintf(pcStr,"%s",tmp);
    }
    //printf("2file_name %s\n",file_name);
    printf("static const u8  %s[ ] =  { \n",file_name);
#endif

//new add end
    int imgSize = 0;
    fseek(bmp, 0x22, SEEK_SET);
    fread(&imgSize, sizeof(int), 1, bmp);

    unsigned char *buffer = (unsigned char *)malloc(imgSize + fill * w*3);
    //memset(buffer, 0xff, imgSize + fill*w*3);
    memset(buffer, 0x00, imgSize + fill*w*3);
    fseek(bmp, 54, SEEK_SET);
    fread(buffer, width * height * 3, 1, bmp);          // 填充在下方
    //fread(buffer + fill/2 * w *3, imgSize, 1, bmp); // 上下填充
//    printf("size %d\n", imgSize);

    int i, j;
    int iCount = 0;
  
//    for (i = 0; i < h; ++i) {
    for (i = h - 1; i >= 0 ; i--){
        for (j = 0; j < w - 1; ++j) {
            unsigned char byte = 0;
#if 0
            byte |= getBit(buffer[3*(j + w*(i*8))]);
            byte |= getBit(buffer[3*(j + w*(i*8+1))]) << 1;
            byte |= getBit(buffer[3*(j + w*(i*8+2))]) << 2;
            byte |= getBit(buffer[3*(j + w*(i*8+3))]) << 3;
            byte |= getBit(buffer[3*(j + w*(i*8+4))]) << 4;
            byte |= getBit(buffer[3*(j + w*(i*8+5))]) << 5;
            byte |= getBit(buffer[3*(j + w*(i*8+6))]) << 6;
            byte |= getBit(buffer[3*(j + w*(i*8+7))]) << 7;
#else
            byte |= getBit(buffer[3*(j + w*(i*8))])<< 7;
            byte |= getBit(buffer[3*(j + w*(i*8+1))]) << 6;
            byte |= getBit(buffer[3*(j + w*(i*8+2))]) << 5;
            byte |= getBit(buffer[3*(j + w*(i*8+3))]) << 4;
            byte |= getBit(buffer[3*(j + w*(i*8+4))]) << 3;
            byte |= getBit(buffer[3*(j + w*(i*8+5))]) << 2;
            byte |= getBit(buffer[3*(j + w*(i*8+6))]) << 1;
            byte |= getBit(buffer[3*(j + w*(i*8+7))]);
#endif
//            byte ^= 0xff;
            //byte = ~byte;
            printf("0x%02x, ", byte);
            iCount++;
            if (iCount%16 == 0)
            {
                 printf("\n");
            }
        }
    }

    printf("};\n");
    printf("\n");
    free(buffer);
    fclose(bmp);


    FILE *fp = fopen("out_array_info", "a");
    if(fp != 0)
    {
        char str[1024];
        snprintf(str,sizeof(str),"{,%d,%d,sizeof(%s),%s},\n",width,height,file_name,file_name);
//        snprintf(str,sizeof(str),"{sizeof(%s)",file_name);
        //printf("str %s\n",str);
        //fseek(fp,0L,SEEK_END);
        fprintf(fp,"%s",str);
    }

    fclose(fp);

    FILE *fp1 = fopen("out_array_enum", "a");
    if(fp1 != 0)
    {
        char str[1024];
        for(unsigned int i = 0; i < strlen(file_name);i++)
        {
            if(file_name[i] >= 'a' && file_name[i] <= 'z')
            {
                file_name[i] -= 32;
            }
        }
        snprintf(str,sizeof(str),"ICON_%s,\n",file_name);
//        snprintf(str,sizeof(str),"{sizeof(%s)",file_name);
        //printf("str %s\n",str);
        //fseek(fp,0L,SEEK_END);
        fprintf(fp1,"%s",str);
    }

    fclose(fp1);


    return 0;
}
